package diplom.uabs.dealing.dealing;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

import java.util.concurrent.TimeUnit;

/**
 * Created by RoM4eG on 26.10.2014.
 */
public class LoadData extends AsyncTask<String, Integer, Void> {
    private ProgressDialog pd;
    private Boolean isCnacel = false;



    public void setDialog(ProgressDialog dialog) {
        pd = dialog;
    }

    public void setCnacel() {

        isCnacel = true;

    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.setIndeterminate(false);
    }


    @Override
    protected Void doInBackground(String... params) {
        for (int i =0; i<10; i++) {
            if (isCnacel) {
                return null;
            }
            publishProgress(i);
            Log.d("qwe", "number = "+i);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        pd.incrementProgressBy(10);

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        pd.incrementProgressBy(9999999); //maximum progress - 100%
        pd.dismiss();
    }
}
